<template>
  <div class="login_class">


      <el-row type="flex" justify="center" align='middle' class="login_row">
        <el-form :model="userinfo" :rules="rules" ref="userinfo" label-width="100px">
          <el-form-item label="账号" prop="username">
            <el-input placeholder="请输入账号" v-model="userinfo.username" clearable></el-input>
          </el-form-item>

          <el-form-item label="密码" prop="password">
            <el-input placeholder="请输入密码" v-model="userinfo.password" show-password></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="onLogin('userinfo')" style="width: 100%;">登陆</el-button>
            <!-- <el-button>注册</el-button> -->
          </el-form-item>
        </el-form>
      </el-row>



  </div>
</template>

<script>
  export default{
    name:"",
    data(){
      return{
        userinfo:{
          username:"",
          password:""
        },
        rules: {
          username:[
            { required: true, message: '请输入账号', trigger: 'blur' },
            { min: 3, max: 20, message: '长度在 0 到 20 个字符', trigger: 'blur' }
          ],
          password:[
            { required: true, message: '请输入密码', trigger: 'blur' }
          ]
        }

      }
    },
    methods:{
      onLogin(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {

            // 校验通过，可以提交
            let _this = this
            _this.$axios.post( _this.$url + 'login/', {
               username: _this.userinfo.username,
               password: _this.userinfo.password
            }).then(function (res) {
              // console.log("==============",res)

              if(res.data.code == 2000){
                // console.log("登录成功")
                _this.$store.commit('userLogin', {user_id:res.data.user_id, token: res.data.token})

                const loading = _this.$loading({
                  lock: true,
                  text: '登陆成功！正在跳转...',
                  spinner: 'el-icon-loading',
                  background: 'rgba(0, 0, 0, 0.7)'
                });
                setTimeout(() => {
                  loading.close();
                  _this.$router.replace({name:"Vbody"})
                }, 2000);

                return;
              }

              _this.$message.error('账号或密码错误！请重试...');

            }).catch(function (error) {
              console.log(error);
            });

          } else {
            console.log('error submit!!');
            return false;
          }

        });
      }
    },
    mounted() {
      console.log(this.$data.header_key)
    }
  }
</script>

<style>
  .login_class{
    height: 100%;
    width: 100%;
  }
  .login_row{
    height: 100%;
  }
</style>
